from browser . logic import browser_logic

class mediator_fake :
    def __init__ ( self ) :
        self . _browser_logic = None
        self . _knowledge_bases_fetched = None
        self . _knowledge_base_receiving_callback = None
        self . _all_knowledge_bases = [ ]
        self . _entry_text = None
        self . _entry_text_on_browser_action = [ ]
        self . _entry_focused = False
        self . _browser_action = False
        self . _bold_table_row_history = [ ]
        self . _title_text_in_browser = [ ]
    def add_title_label_to_browser ( self , title_text ) :
        pass
    def add_table_to_browser ( self ) :
        pass
    def add_return_button_to_browser ( self ) :
        pass
    def add_action_button_to_browser ( self , action_text ) :
        pass
    def add_title_row_to_browser ( self , number , name , creation_time , modification_time , size ) :
        pass
    def add_existing_row_to_browser ( self , number , name , creation_time , modification_time , size ) :
        pass
    def add_empty_row_to_browser ( self ) :
        pass
    def add_action_row_to_browser ( self ) :
        pass
    def set_entry_text_in_browser ( self , text ) :
        self . _entry_text = text
    def entry_text_in_browser_request ( self ) :
        self . _browser_logic . entry_text_in_browser_reply ( self . _entry_text )
    def focus_entry_in_browser ( self ) :
        self . _entry_focused = True
    def unfocus_entry_in_browser ( self ) :
        self . _entry_focused = False
    def select_table_row ( self , row ) :
        pass
    def unselect_table_row ( self , row ) :
        pass
    def localization_browser_number ( self ) :
        pass
    def localization_browser_existing_name ( self ) :
        pass
    def localization_browser_creation_time ( self ) :
        pass
    def localization_browser_modification_time ( self ) :
        pass
    def localization_browser_size ( self ) :
        pass
    def fetch_all_knowledge_bases ( self ) :
        self . _knowledge_bases_fetched = True
        for kb in self . _all_knowledge_bases :
            self . _knowledge_base_receiving_callback ( * kb )
    def on_browser_action ( self ) :
        self . _browser_action = True
        self . _entry_text_on_browser_action += [ self . _entry_text ]
    def bold_table_row ( self , row ) :
        self . _bold_table_row_history += [ [ mediator_fake . bold_table_row , row ] ]
    def unbold_table_row ( self , row ) :
        self . _bold_table_row_history += [ [ mediator_fake . unbold_table_row , row ] ]
    def set_title_text_in_browser ( self , text ) :
        self . _title_text_in_browser += [ text ]
    
class browser_logic_on_key_return_tests :
    'browser_logic_on_key_return'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _browser_logic = browser_logic ( self . _mediator )
        self . _mediator . _browser_logic = self . _browser_logic
    def test_on_key_return_copy_active_item_text_to_entry ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time2' , None ]
            , [ 'name2' , None , 'time1' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_down ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'name2' )
    def test_on_key_return_focus_entry ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time2' , None ]
            , [ 'name2' , None , 'time1' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_down ( )
        self . _mediator . _entry_focused = None
        self . _browser_logic . on_key_return ( )
        self . _test . assume_true ( self . _mediator . _entry_focused )
    def test_on_key_return_focus_entry_when_configured_to_remove_action_part_and_no_entries ( self ) :
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_false ( self . _mediator . _entry_focused )
    def test_on_key_return_focus_entry_when_configured_to_remove_action_part_and_there_are_entries ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time2' , None ]
            , [ 'name2' , None , 'time1' , None ]
            ]
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_down ( )
        self . _mediator . _entry_focused = False
        self . _browser_logic . on_key_return ( )
        self . _test . assume_true ( self . _mediator . _entry_focused )
    def test_on_key_return_dont_rewrite_item_text_on_subsequent_presses ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time2' , None ]
            , [ 'name2' , None , 'time1' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_down ( )
        self . _mediator . _entry_text = 'my first text'
        self . _browser_logic . on_key_return ( )
        self . _mediator . _entry_text = 'my second text'
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'my second text' )
    def test_on_key_return_dont_focus_entry_on_subsequent_presses ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time2' , None ]
            , [ 'name2' , None , 'time1' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_down ( )
        self . _browser_logic . on_key_return ( )
        self . _mediator . _entry_focused = None
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_focused , None )
    def test_on_key_return_dont_rewrite_item_text_when_pressed_right_after_show ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'my name' , None , 'my time' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _mediator . _entry_text = 'my text'
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'my text' )
    def test_on_key_return_copy_active_item_text_to_entry_after_key_up ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time1' , None ]
            , [ 'name2' , None , 'time2' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_up ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'name1' )
    def test_on_key_return_dont_focus_entry_before_show ( self ) :
        self . _mediator . _entry_focused = 'my focused'
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_focused , 'my focused' )
    def test_on_key_return_dont_focus_entry_after_hide ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time1' , None ]
            , [ 'name2' , None , 'time2' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_up ( )
        self . _browser_logic . hide_browser ( )
        self . _mediator . _entry_focused = None
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_focused , None )
    def test_on_key_return_when_no_items ( self ) :
        self . _browser_logic . show_browser ( )
        self . _mediator . _entry_focused = None
        self . _mediator . _entry_text = ''
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_focused , None )
    def test_on_key_return_browser_action_after_double_return ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'my name' , None , None , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_true ( self . _mediator . _browser_action )
    def test_on_key_return_no_browser_action_after_unfocus_entry_and_return ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'my name' , None , None , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_browser_entry_unfocus ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_false ( self . _mediator . _browser_action )
    def test_on_key_return_browser_action_after_unfocus_and_focus_entry_then_return ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'my name' , None , None , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _browser_logic . on_browser_entry_unfocus ( )
        self . _browser_logic . on_browser_entry_focus ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_true ( self . _mediator . _browser_action )
    def test_on_key_return_make_only_selected_row_bold ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time3' , None ]
            , [ 'name2' , None , 'time2' , None ]
            , [ 'name3' , None , 'time1' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _mediator . _bold_table_row_history = [ ]
        self . _browser_logic . on_key_down ( )
        self . _browser_logic . on_key_return ( )
        self . _browser_logic . on_key_down ( )
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _bold_table_row_history ,
            [ [ mediator_fake . unbold_table_row , 1 ]
            , [ mediator_fake . bold_table_row , 2 ]
            , [ mediator_fake . unbold_table_row , 2 ]
            , [ mediator_fake . bold_table_row , 3 ]
            ] )
    def test_on_key_return_filter_item_text ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ 'my name' , None , 'my time' , None ] ]
        self . _browser_logic . show_browser ( )
        self . _mediator . _entry_text = '  !@#$%^&*()_+   valid    name  part123;\',./\\][}{   '
        self . _browser_logic . on_key_return ( )
        self . _test . assume_equal ( self . _mediator . _entry_text_on_browser_action , [ 'valid name part123' ] )
    def test_on_key_return_check_uniqueness ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'my name1' , None , 'my time1' , None ]
            , [ 'my name2' , None , 'my time2' , None ]
            ]
        self . _browser_logic . configure_only_unique_names_mode_for_browser ( 'my already exists' )
        self . _browser_logic . show_browser ( )
        self . _mediator . _entry_text = '   !@#$my   name1   !@#$ '
        self . _browser_logic . on_key_return ( )
        self . _test . assume_empty ( self . _mediator . _entry_text_on_browser_action )
        self . _test . assume_equal ( self . _mediator . _title_text_in_browser , [ 'my already exists' ] )
